package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author agen
 * @since 2020-04-17
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;

    public void insertUser(User user) {
        log.info("----------------插入一条记录： insert user----------------");
        userMapper.insert(user);
    }

    public void serviceSave(User user) {
        log.info("----------------service插入一条记录： save user----------------");
        save(user);
    }

    public void selectTest() {
        log.info("----------------按主键查询： where id=2----------------");
        User user = userMapper.selectById(1);
        log.info(user.toString());

        log.info("----------------按主键查询： where id=2 ? and age=?----------------");
        User user1 = new User();
        user1.setId(2L);
        user1.setAge(20);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>(user1);
        User user2 = userMapper.selectOne(queryWrapper);
        log.info(user2.toString());

        log.info("----------------按主键查询： where age=?----------------");
        User user3 = new User();
        user3.setAge(18);
        QueryWrapper<User> queryWrapper1 = new QueryWrapper<>(user3);
        List<User> users = userMapper.selectList(queryWrapper1);
        log.info("age=18:"+users.toString());

        log.info("----------------按主键查询： where age=?----------------");
        Map<String, Object> columnMap=new HashMap<>();
        columnMap.put("age",18);
        List<User> users1 = userMapper.selectByMap(columnMap);
        log.info("age=18:"+users1.toString());

        log.info("----------------按条件查询：LIKE '%a%' and age<22----------------");
        User user4 = new User();
        QueryWrapper<User> queryWrapper2=new QueryWrapper<>();
        QueryWrapper<User> lt = queryWrapper.like("name", "a").lt("age", 22);
        List<User> users2 = userMapper.selectList(lt);
        log.info("age=18:"+users2.toString());

        log.info("----------------按条件查询-lambda：name=agen6----------------");
        List<User> agen = userMapper.selectList(new QueryWrapper<User>().lambda().eq(User::getName, "agen6"));
        log.info("age=18:"+agen.toString());

        log.info("----------------按Page查询-lambda 升序----------------");
        Page<User> page =new Page<>(1,3);
        Page<User> userPage = userMapper.selectPage(page, Wrappers.<User>lambdaQuery().eq(User::getAge, 18).orderByAsc(User::getId));
        List<User> records = userPage.getRecords();
        long total = userPage.getTotal();
        log.info(records.toString());
        log.info("total="+total);

    }

    public  void  pageTest() {
        log.info("----------------baseMapper 自带分页----------------");
        Page<User> page =new Page<>(1,5);
        page.addOrder(OrderItem.asc("age"));
        Page<User> userPage = userMapper.selectPage(page, Wrappers.<User>lambdaQuery().eq(User::getAge, 20));
        log.info("userPage="+userPage.getRecords().toString());
        log.info("total="+userPage.getTotal()+"current="+userPage.getCurrent());
    }

   public void selectUserPage() {
        log.info("---------------- 自定义sql---------------");
       List<User> users = userMapper.selectByName("Tom");
       log.info("users="+users.toString());
    }
}


